/*
Checar parcelas comparando com a venda
*/
DECLARE @DATA DATETIME

SET @DATA = CONVERT(CHAR(8),GETDATE()-1,112) 

SELECT * FROM
(SELECT FF.CODIGO_FILIAL, SUM(VALOR_PAGO1-VALOR_PAGO2) AS DIF FROM
(SELECT 
B.CODIGO_FILIAL,
SUM(B.VALOR) AS VALOR_PAGO1
FROM 
LOJA_VENDA A
INNER JOIN LOJA_VENDA_PARCELAS B ON A.CODIGO_FILIAL=B.CODIGO_FILIAL AND A.TERMINAL=B.TERMINAL AND A.LANCAMENTO_CAIXA=B.LANCAMENTO_CAIXA
WHERE A.DATA_VENDA=@DATA 
GROUP BY B.CODIGO_FILIAL
) FF
--UNION ALL
INNER JOIN ( 
SELECT 
A.CODIGO_FILIAL,
SUM(A.VALOR_PAGO) AS VALOR_PAGO2
FROM
LOJA_VENDA A
WHERE A.DATA_VENDA=@DATA 
GROUP BY A.CODIGO_FILIAL
) DD ON DD.CODIGO_FILIAL=FF.CODIGO_FILIAL
GROUP BY FF.CODIGO_FILIAL) CC
WHERE DIF!=0